import { Module } from 'vuex'

import { RootState, SearchMusicType, HotPlaylistAction } from './types'
import {
  getBanners,
  getHotPlaylist,
  getNewAlbum,
  getRanks,
  getRanksDetails,
  getPartSingers,
  getHotestAnchor
} from '@/api'

const searchMusic: Module<SearchMusicType, RootState> = {
  namespaced: true,
  state: {
    banner: [],
    firstBanner: '',
    hotPlaylist: [],
    newAlbum_first: [],
    newAlbum_second: [],
    ranks: [],
    rankParts: [],
    rankPartsId: [],
    rankPartsDetails: [],
    inSingers: [],
    hotestAnchor: []
  },
  mutations: {
    setBannersMutations(state, data: any[]) {
      state.banner = data
      state.firstBanner = data[0].imageUrl + '?imageView&blur=40x20'
      // console.log(state.firstBanner)
    },
    setHotPlaylistMutations(state, data: any[]) {
      state.hotPlaylist.push(data)
    },
    setNewAlbumMutations(state, data: any[]) {
      state.newAlbum_first = data.slice(2, 7)
      state.newAlbum_second = data.slice(7)
      // console.log(state.newAlbum_first, state.newAlbum_second)
    },
    setRanksMutations(state, data: any[]) {
      state.ranks = data
      state.rankParts = data.slice(0, 3)
      state.rankParts.forEach((item) => state.rankPartsId.push(item.id))
    },
    setRanksDetailMutations(state, data: any) {
      state.rankPartsDetails.push(data)
    },
    // 设置入驻歌手信息
    setInSingersMutations(state, data: any[]) {
      state.inSingers = data
    },
    // 设置最热主播信息
    setHotestAnchor(state, data: any[]) {
      state.hotestAnchor = data
    }
  },
  actions: {
    // 获取banner
    async getBannersAction({ commit }) {
      const res = await getBanners()
      // console.log(res.banners)
      commit('setBannersMutations', res.banners)
    },
    // 获取最热歌单
    async getHotPlaylistAction({ commit }, payload: HotPlaylistAction) {
      const { limit, offset } = payload
      const res = await getHotPlaylist(limit, offset)
      // console.log(res.playlists)
      commit('setHotPlaylistMutations', res.playlists)
    },
    // 获取最新专辑
    async getNewAlbumAction({ commit }) {
      const res = await getNewAlbum()
      // console.log(res.albums)
      commit('setNewAlbumMutations', res.albums)
    },
    // 获取全部排名
    async getRanksAction({ commit }) {
      const res = await getRanks()
      console.log(res.list)
      commit('setRanksMutations', res.list)
    },
    // 获取全部排名细节
    async getRanksDetailsAction({ commit }, payload: number) {
      const res = await getRanksDetails(payload)
      // console.log(res.playlist)
      commit('setRanksDetailMutations', res.playlist)
    },
    //获取入驻歌手
    async getInSinger({ commit }) {
      const res = await getPartSingers()
      // console.log(res.artists)
      commit('setInSingersMutations', res.artists)
    },
    // 获取最热主播
    async getHotAnchor({ commit }) {
      const res = await getHotestAnchor()
      // console.log(res.data.list)
      commit('setHotestAnchor', res.data.list)
    }
  }
}

export default searchMusic
